﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Globalization;
using System.ComponentModel;
using System.Collections.Specialized;

namespace MyLib.Web.UI {
	/// <summary>
	/// 
	/// </summary>
	[DefaultProperty("Text")]
	[DefaultEvent("CheckedChanged")]
	[ToolboxData("<{0}:MyButton runat=\"server\" ></{0}:MyButton>")]
	public class MyCheckBox : WebControl, ICheckBoxControl, IPostBackDataHandler {
		/// <summary>
		/// 
		/// </summary>
		[DefaultValue(false)]
		[Themeable(false)]
		[Category("Behavior")]
		[Description("CheckBox_AutoPostBack")]
		public virtual bool AutoPostBack {
			get {
				object obj = this.ViewState["AutoPostBack"];
				return obj != null && (bool)obj;
			}
			set {
				this.ViewState["AutoPostBack"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[DefaultValue(false)]
		[Themeable(false)]
		[Category("Behavior")]
		[Description("AutoPostBackControl_CausesValidation")]
		public virtual bool CausesValidation {
			get {
				object obj = this.ViewState["CausesValidation"];
				return obj != null && (bool)obj;
			}
			set {
				this.ViewState["CausesValidation"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[Bindable(true)]
		[DefaultValue("")]
		[Localizable(true)]
		[Category("Appearance")]
		[Description("CheckBox_Text")]
		public virtual string Text {
			get {
				string text = (string)this.ViewState["Text"];
				if (text != null) {
					return text;
				}
				return string.Empty;
			}
			set {
				this.ViewState["Text"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[DefaultValue(TextAlign.Right)]
		[Category("Appearance")]
		[Description("WebControl_TextAlign")]
		public virtual TextAlign TextAlign {
			get {
				object obj = this.ViewState["TextAlign"];
				if (obj != null) {
					return (TextAlign)obj;
				}
				return TextAlign.Right;
			}
			set {
				if (value < TextAlign.Left || value > TextAlign.Right) {
					throw new ArgumentOutOfRangeException("value");
				}
				this.ViewState["TextAlign"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[DefaultValue("")]
		[Themeable(false)]
		[Category("Behavior")]
		[Description("PostBackControl_ValidationGroup")]
		public virtual string ValidationGroup {
			get {
				string text = (string)this.ViewState["ValidationGroup"];
				if (text != null) {
					return text;
				}
				return string.Empty;
			}
			set {
				this.ViewState["ValidationGroup"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected virtual void OnCheckedChanged(EventArgs e) {
			if (CheckedChanged != null) {
				CheckedChanged(this, e);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="postDataKey"></param>
		/// <param name="postCollection"></param>
		/// <returns></returns>
		protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) {
			string value = postCollection[postDataKey];

			bool flag = !string.IsNullOrEmpty(value);

			bool oldChecked = Checked;
			Checked = flag;
			return flag != oldChecked;
		}

		/// <summary>
		/// 
		/// </summary>
		protected virtual void RaisePostDataChangedEvent() {
			if (this.AutoPostBack && !this.Page.IsPostBackEventControlRegistered) {
				this.Page.AutoPostBackControl = this;
				if (this.CausesValidation) {
					this.Page.Validate(this.ValidationGroup);
				}
			}
			this.OnCheckedChanged(EventArgs.Empty);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnPreRender(EventArgs e) {
			base.OnPreRender(e);

			if (this.Page != null && base.IsEnabled) {
				this.Page.RegisterRequiresPostBack(this);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="writer"></param>
		protected override void Render(System.Web.UI.HtmlTextWriter writer) {
			this.AddAttributesToRender(writer);
			if (this.Page != null) {
				this.Page.VerifyRenderingInServerForm(this);
			}

			if (!base.IsEnabled) {
				if (!this.Enabled && !string.IsNullOrEmpty(WebControl.DisabledCssClass)) {
					if (string.IsNullOrEmpty(this.CssClass)) {
						base.ControlStyle.CssClass = WebControl.DisabledCssClass;
					} else {
						base.ControlStyle.CssClass = WebControl.DisabledCssClass + " " + this.CssClass;
					}
				}
			}
			if (base.ControlStyleCreated) {
				Style controlStyle = base.ControlStyle;
				if (!controlStyle.IsEmpty) {
					controlStyle.AddAttributesToRender(writer, this);
				}
			}
			string toolTip = this.ToolTip;
			if (toolTip.Length > 0) {
				writer.AddAttribute(HtmlTextWriterAttribute.Title, toolTip);
			}

			if (HasAttributes) {
				Attributes.Remove("value");
				Attributes.Remove("onclick");

				if (Attributes.Count != 0) {
					Attributes.AddAttributes(writer);
				}
			}

			writer.RenderBeginTag(HtmlTextWriterTag.Label);

			if (!string.IsNullOrWhiteSpace(Text)) {
				if (this.TextAlign == TextAlign.Left) {
					writer.WriteEncodedText(Text);
					this.RenderInputTag(writer);
				} else {
					this.RenderInputTag(writer);
					writer.WriteEncodedText(Text);
				}
			} else {
				this.RenderInputTag(writer);
			}

			writer.RenderEndTag();
		} // end of Render.

		/// <summary>
		/// 
		/// </summary>
		/// <param name="writer"></param>
		private void RenderInputTag(HtmlTextWriter writer) {
			writer.AddAttribute(HtmlTextWriterAttribute.Type, "checkbox");
			if (this.UniqueID != null) {
				writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
			}
			if (this.Checked) {
				writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
			}
			if (!base.IsEnabled && this.SupportsDisabledAttribute) {
				writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
			}
			if (this.AutoPostBack && this.Page != null) {
				PostBackOptions postBackOptions = new PostBackOptions(this, string.Empty);
				if (this.CausesValidation && this.Page.GetValidators(this.ValidationGroup).Count > 0) {
					postBackOptions.PerformValidation = true;
					postBackOptions.ValidationGroup = this.ValidationGroup;
				}
				if (this.Page.Form != null) {
					postBackOptions.AutoPostBack = true;
				}

				writer.AddAttribute(HtmlTextWriterAttribute.Onclick,
					Page.ClientScript.GetPostBackEventReference(postBackOptions, true));
			} else {
				if (this.Page != null) {
					this.Page.ClientScript.RegisterForEventValidation(this.UniqueID);
				}
			}
			string accessKey = this.AccessKey;
			if (accessKey.Length > 0) {
				writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, accessKey);
			}
			int tabIndex = (int)this.TabIndex;
			if (tabIndex != 0) {
				writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, tabIndex.ToString(NumberFormatInfo.InvariantInfo));
			}

			writer.RenderBeginTag(HtmlTextWriterTag.Input);
			writer.RenderEndTag();
		} // end of RenderInputTag.

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		private static string EnsureEndWithSemiColon(string value) {
			if (value != null) {
				int length = value.Length;
				if (length > 0 && value[length - 1] != ';') {
					return value + ";";
				}
			}
			return value;
		}

		#region ICheckBoxControl

		/// <summary>
		/// 
		/// </summary>
		[Bindable(true, BindingDirection.TwoWay)]
		[DefaultValue(false)]
		[Themeable(false)]
		[Description("CheckBox_Checked")]
		public virtual bool Checked {
			get {
				object obj = this.ViewState["Checked"];
				return obj != null && (bool)obj;
			}
			set {
				this.ViewState["Checked"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[Category("Action")]
		[Description("Control_OnServerCheckChanged")]
		public event EventHandler CheckedChanged;

		#endregion ICheckBoxControl

		#region IPostBackHandler

		/// <summary>
		/// 
		/// </summary>
		/// <param name="postDataKey"></param>
		/// <param name="postCollection"></param>
		/// <returns></returns>
		bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection) {
			return this.LoadPostData(postDataKey, postCollection);
		}

		/// <summary>
		/// 
		/// </summary>
		void IPostBackDataHandler.RaisePostDataChangedEvent() {
			this.RaisePostDataChangedEvent();
		}

		#endregion

		/// <summary>
		/// 
		/// </summary>
		public MyCheckBox()
			: base(HtmlTextWriterTag.Label) {
		} // end of MyCheckBox.
	} // end of MyCheckBox.
}
